package com.huike.web.controller.system.dict;

import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: fwh
 * @Date: 2023/07/11/15:13
 * @Description:
 */
@RestController
@RequestMapping("/system/dict/data")
@Slf4j
public class SysDictDataController extends BaseController {

    @Resource
    private ISysDictDataService dataService;

    /**
     * 新增字典
     *
     * @param sysDictData
     * @return
     */
    @PostMapping
    @ApiOperation("")
    public AjaxResult<?> add(@RequestBody SysDictData sysDictData) {
        log.info("新增字典:{}", sysDictData);
        dataService.add(sysDictData);
        return AjaxResult.success();

    }

    @PutMapping
    public AjaxResult<?> update(@RequestBody SysDictData sysDictData) {
        log.info("修改字典:{}", sysDictData);
        dataService.update(sysDictData);
        return AjaxResult.success();
    }

    @GetMapping("/type/{dictType}")
    public AjaxResult<List<SysDictData>> select(@PathVariable String dictType) {
        log.info("根据字典类型查询字典数据信息-----------------------------------------------------------------:{}", dictType);
        List<SysDictData> list = dataService.select(dictType);
        return AjaxResult.success(list);
    }

    @GetMapping("{/dictCode}")
    public AjaxResult selectDictCode(@PathVariable Integer dictCode) {
        log.info("查询字典数据详细:{}", dictCode);
        SysDictData sysDictData = dataService.selectDictCode(dictCode);
        return AjaxResult.success(sysDictData);
    }

    /**
     * 批量删除字典类型
     * @param dictCodes
     * @return
     */
    @DeleteMapping("/{dictCodes}")
    @Log
    public AjaxResult delect(@PathVariable Integer dictCodes){
        dataService.delect(dictCodes);
        return AjaxResult.success();
    }

    /**
     * 字典列表
     * @param list
     * @return
     */
    @GetMapping("/list")
    @Log
    public AjaxResult<List<SysDictData>> selectList(SysDictData list){
       List<SysDictData> dictData= dataService.selectList(list);
       return AjaxResult.success(dictData);
    }

    @GetMapping("/export")
    public AjaxResult export(SysDictData sysDictData){
        List<SysDictData>list=dataService.selectDictDataList(sysDictData);
        ExcelUtil<SysDictData> sysDictDataExcelUtil = new ExcelUtil<>(SysDictData.class);
        return sysDictDataExcelUtil.exportExcel(list,"字典数据");
    }
}
